package com.example.demo.queue.handler;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.entity.dto.LoginerDTO;
import com.example.demo.queue.message.TestEvent;
import com.lmax.disruptor.EventHandler;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class TestEventHandler implements EventHandler<TestEvent> {

    @Override
    public void onEvent(TestEvent event, long sequence, boolean endOfBatch) {
        String threadName = Thread.currentThread().getName();
        String result;
        String formatStr = "id:%s，name:%s，asyncName:%s，username:%s，password:%s";
        if (event.getType() == 200) {
            LoginerDTO user = (LoginerDTO) event.getData();
            result = String.format(formatStr, event.getId(), event.getName(), threadName, user.getUsername(), user.getPassword());
        }
        else {
            JSONObject js = (JSONObject) event.getData();
            result = String.format(formatStr, event.getId(), event.getName(), threadName, "无", js.getString("name"));
        }

        System.out.println(result);
    }
}
